package com.example.springbootredis.controller;


import com.example.springbootredis.entiy.Region;
import com.example.springbootredis.service.RegionService;
import com.example.springbootredis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("region")
@CrossOrigin(maxAge = 3600)
public class RegionController {

    @Autowired
    private RegionService regionService;

    @GetMapping("provinces")
    public JsonResult getProvinces() {
        List<Region> provinces = regionService.findByLevel(1);
        return JsonResult.success("获取成功", provinces);
    }

    @GetMapping("cities")
    public JsonResult getCities(@RequestParam Integer provinceId) {
        List<Region> cities = regionService.findByParentId(provinceId);
        return JsonResult.success("获取成功", cities);
    }

    @GetMapping("districts")
    public JsonResult getDistricts(@RequestParam Integer cityId) {
        List<Region> districts = regionService.findByParentId(cityId);
        return JsonResult.success("获取成功", districts);
    }
}